package org.streets.workflow.engine.mem;

import java.util.ArrayList;
import java.util.List;

import org.streets.workflow.engine.ILoop;
import org.streets.workflow.engine.INode.NodeType;
import org.streets.workflow.engine.event.IEdgeEventListener;
import org.streets.workflow.engine.plugin.IPlugable;
import org.streets.workflow.engine.plugin.INodeNetExtension;
import org.streets.workflow.model.net.WFLoop;


/**
 *
 */
public class Loop extends AbstractEdge implements ILoop ,IPlugable {
    
    public transient static final String Extension_Target_Name = "org.fireflow.kernel.LoopInstance";
    public transient static List<String> Extension_Point_Names = new ArrayList<String>();
    public transient static final String Extension_Point_LoopInstanceEventListener = "LoopInstanceEventListener";

    static {
        Extension_Point_Names.add(Extension_Point_LoopInstanceEventListener);
    }


    private transient WFLoop loop = null;

    public Loop(WFLoop lp){
        this.loop = lp;
    }

    public String getEdgeId() {
        return loop.getId();
    }

    @SuppressWarnings("unchecked")
    public WFLoop getEdgeModel() {
        return loop;
    }
    
    public int getWeight() {
        if (weight == 0) {
            if (leavingNode.getNodeType() == NodeType.Router){
                weight=((SyncRouter)this.leavingNode).getVolume();
                  //如果后继结点是同步器节点，那么权值=后继结点的容量（这样就可以直接执行后继activity了）
            } else if (leavingNode.getNodeType() == NodeType.Start){
                weight = ((StartNode)this.leavingNode).getVolume();
                //如果后继结点是开始节点，那么权值=开始节点的容量
            } else if (leavingNode.getNodeType() == NodeType.End){
                weight = ((EndNode)this.leavingNode).getVolume();
                //如果后继结点是结束节点，那么权值=结束节点的容量
            }
        }
        return weight;
    }
//
//    /* (non-Javadoc)
//     * @see org.fireflow.kernel.IEdgeInstance#take(org.fireflow.kernel.IToken)
//     */
//    public boolean take(IToken token) throws WorkflowException {
//
//        boolean oldAlive = token.isAlive();
//
//        EdgeEvent e = new EdgeEvent(this);
//        e.setToken(token);
//        e.setEventType(EdgeEvent.ON_TAKING_THE_TOKEN);
//
//        for (int i = 0; this.eventListeners != null && i < this.eventListeners.size(); i++) {
//            IEdgeEventListener listener =  this.eventListeners.get(i);
//            listener.onEdgeEventFired(e);
//        }
//
//        boolean newAlive = token.isAlive();
//
//        if (!newAlive){//循环条件不满足，则恢复token的alive标示
//            token.setAlive(oldAlive);
//            return newAlive;
//        }else{//否则流转到下一个节点
//
//            INode nodeInst = this.getLeavingNode();
//
//            token.setValue(this.getWeight());
//            nodeInst.fire(token);//触发同步器节点
//            return newAlive;
//        }
//    }


    public void setLoop(WFLoop arg0){
        this.loop = arg0;
    }

    @SuppressWarnings("static-access")
	public String getExtensionTargetName() {
        return this.Extension_Target_Name;
    }

    @SuppressWarnings("static-access")
	public List<String> getExtensionPointNames() {
        return this.Extension_Point_Names;
    }

    public void register(INodeNetExtension extension) throws RuntimeException {
        if (!Extension_Target_Name.equals(extension.getExtentionTargetName())) {
            return;
        }
        if (Extension_Point_LoopInstanceEventListener.equals(extension.getExtentionPointName())) {
            if (extension instanceof IEdgeEventListener) {
                this.eventListeners.add((IEdgeEventListener) extension);
            } else {
                throw new RuntimeException("Error:When construct the TransitionInstance,the extension MUST be a instance of ITransitionInstanceEventListener");
            }
        }
    }

}
